package com.ruoyi.common.core.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 集合方法类
 * @Author: j.zh
 * @Date: 2024/6/13
 * @Version: 4.0.0
 **/
public class ListUtils {

    /**
     * 按指定大小，分隔集合，将集合按规定个数分为n个部分
     *
     * @param list
     * @param subListSize
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int subListSize) {

        if (list == null || list.isEmpty() || subListSize < 1) {
            return Collections.emptyList();
        }

        List<List<T>> result = new ArrayList<>();

        int size = list.size();
        int subListNum = (size + subListSize - 1) / subListSize;

        for (int i = 0; i < subListNum; i++) {
            List<T> subList = list.subList(i * subListSize, ((i + 1) * subListSize > size ? size : subListSize * (i + 1)));
            result.add(subList);
        }

        return result;
    }
}
